import 'dart:async';
import 'dart:io';
import 'package:date_format/date_format.dart' as Sa;
import 'package:agora_rtc_engine/agora_rtc_engine.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:videotop/sql/databasehelper.dart';
import 'package:videotop/sql/msg_table.dart';
import 'package:videotop/sql/user_table.dart';
import 'package:videotop/widget/report.dart';
import 'package:videotop/widget/spacePage.dart';
import 'package:videotop/widget/videoPage.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../evenbus.dart';
import '../myinfo.dart';
import '../screen_service.dart';
import '../util.dart';
import './buygold.dart';
import 'buyvip.dart';

class Chat extends StatefulWidget {
  UserTable userTable;

  Chat({this.userTable});

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _Chat();
  }
}

class _Chat extends State<Chat> with TickerProviderStateMixin {
  List<Msg> listchat = [];
  String content = '';
  File file;
  Util util;
  MyInfo myInfo;
  ImagePicker picker = ImagePicker();
  TextEditingController textEditingController = TextEditingController();
  ScrollController scrollController = ScrollController();

  var db = DatabaseHelper();

  getNativeLogin() async {
    util.getNativeYuid(widget.userTable.uid);
  }

  @override
  void initState() {
    getNativeLogin();
    Future.delayed(Duration.zero, () {
      getInit();
    });
    super.initState();

    bus.on('chat', (arg) async{
      if (arg.uid == widget.userTable.uid) {
        listchat.add(arg);
        if (mounted) setState(() {});
      }
    });

  }


  upState() {
    db.updateTable(widget.userTable.uid).then((value) {
      if (value > 0) refresh();
    });
  }

  getInit() async {
    List datas = await db.getNewMsg(widget.userTable.uid);
    listchat.clear();
    List<Msg> newlist = [];
    for(int i = 0;i < datas.length ; i++){
      Msg dataListBean = Msg.fromMap(datas[i]);
      newlist.add(dataListBean);
    }
    listchat.addAll(newlist);
    if (mounted) setState(() {});
  }



  @override
  Widget build(BuildContext context) {
    if (listchat.length > 0)
      Timer(
          Duration(milliseconds: 500),
              () => scrollController
              .jumpTo(scrollController.position.maxScrollExtent));
    return Scaffold(
      backgroundColor: Color(0xff3F2C9F),
      body: Container(
        margin: EdgeInsets.only(
            left: 8,
            right: 8,
            bottom: 5,
            top: MediaQuery.of(context).padding.top + 10),
        child: Column(
          children: <Widget>[
            Container(
              margin: EdgeInsets.only(bottom: 20),
              child: Row(
                children: <Widget>[
                  GestureDetector(
                      behavior: HitTestBehavior.opaque,
                      onTap: () {
                        Navigator.pop(context);
                      },
                      child: Container(
                        padding: EdgeInsets.only(
                            left: 10, top: 10, bottom: 10, right: 5),
                        child: Image.asset(
                          'assets/image/back_white.png',
                          width: 20,
                          height: 20,
                        ),
                      )),
                  Container(
                    margin: EdgeInsets.only(left: 20),
                    height: 45,
                    width: 45,
                    child: GestureDetector(
                      child: ClipOval(
                        child: _icon(1),
                      ),
                      onTap: () {
                        Navigator.of(context)
                            .push(MaterialPageRoute(
                            builder: (context) => Space(
                              uid: widget.userTable.uid,
                            )));
                      },
                    ),
                  ),
                  Container(
                    margin: EdgeInsets.only(left: 5),
                    child: Text(
                      widget.userTable.nickname,
                      overflow: TextOverflow.ellipsis,
                      style: TextStyle(
                          fontSize: 16,
                          fontWeight: FontWeight.bold,
                          color: Colors.white),
                    ),
                  ),
                  Expanded(
                      child: Container()
                  ),
                  GestureDetector(
                    behavior: HitTestBehavior.opaque,
                    onTap: sendvideo,
                    child: Image.asset(
                      'assets/image/video_w.png',
                      width: 30,
                      height: 30,
                    ),
                  ),
                  IconButton(
                      icon: Image.asset(
                        'assets/image/dian3w.png',
                        width: 25,
                        height: 25,
                      ),
                      onPressed: () {
                        reblack();
                      }),
                ],
              ),
            ),
            Expanded(child: Container(
                decoration: BoxDecoration(
                    borderRadius: BorderRadius.only(
                        topLeft: Radius.circular(10),
                        topRight: Radius.circular(10)),
                    color: Color(0xff1e154a)),
                child: SingleChildScrollView(
                  controller: scrollController,
                  child: Column(
                    children: [
                      Container(
                        margin: EdgeInsets.only(top: 20),
                        height: ScreenService.width/3,width: ScreenService.width/3,
                        child: ClipOval(
                          child: _icon(2),
                        ),
                      ),
                      Container(
                          margin: EdgeInsets.only(top: 20),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: [
                              Container(
                                constraints: BoxConstraints(maxWidth: 100),
                                child: Text(
                                  "${widget.userTable.nickname}",
                                  maxLines: 1,
                                  overflow: TextOverflow.ellipsis,
                                  style: TextStyle(
                                      fontSize: 26,
                                      fontFamily: 'my_bold',
                                      color: Colors.white),
                                ),
                              ),
                              const SizedBox(
                                width: 10,
                              ),
                              Container(
                                child: Text(
                                  "${util.getAge(DateTime.parse(widget.userTable.birth))}",
                                  style: TextStyle(
                                      fontSize: 26,
                                      fontFamily: 'my_bold',
                                      color: Colors.white),
                                ),
                              ),
                            ],
                          )),
                      Container(
                          margin: EdgeInsets.only(top: 10),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: [
                              Container(
                                constraints: BoxConstraints(maxWidth: 100),
                                child: Text(
                                  "${widget.userTable.country}",
                                  maxLines: 1,
                                  overflow: TextOverflow.ellipsis,
                                  style: TextStyle(
                                      fontSize: 15,
                                      color: Colors.grey),
                                ),
                              ),
                              const SizedBox(
                                width: 10,
                              ),
                              Container(
                                child: Text(
                                  "${util.getConstellation(DateTime.parse(widget.userTable.birth), context)}",
                                  style: TextStyle(
                                      fontSize: 15,
                                      color: Colors.grey),
                                ),
                              ),
                            ],
                          )),
                      Container(
                        margin: EdgeInsets.only(top: 10),
                        child: Text(
                          'You and ${widget.userTable.nickname} are now friends',
                          textAlign: TextAlign.center,
                          style: TextStyle(
                              color: Colors.white,
                              fontSize: 15
                          ),
                        ),
                      ),
                      Container(
                        margin: EdgeInsets.only(top: 10),
                        child: Text(
                          '${Sa.formatDate(DateTime.fromMillisecondsSinceEpoch(int.parse(widget.userTable.times)),[
                            'hh',':','nn',' ','am'
                          ])}',
                          textAlign: TextAlign.center,
                          style: TextStyle(
                              color: Colors.white,
                              fontSize: 15
                          ),
                        ),
                      ),
                      ListView.builder(
                          itemCount: listchat.length,
                          shrinkWrap: true,
                          physics: NeverScrollableScrollPhysics(),
                          itemBuilder: (context, index) {
                            if (listchat[index].type == 0) {
                              return Container(
                                width: MediaQuery.of(context).size.width,
                                padding: EdgeInsets.only(
                                    bottom: 15,
                                    left:
                                    MediaQuery.of(context).size.width * 0.2),
                                child: Row(
                                  mainAxisAlignment: MainAxisAlignment.end,
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: <Widget>[
                                    Container(
                                      margin:
                                      EdgeInsets.only(right: 10, top: 10),
                                      child: Column(
                                        crossAxisAlignment:
                                        CrossAxisAlignment.end,
                                        children: <Widget>[
                                          Visibility(
                                              visible: listchat[index]
                                                  .content
                                                  .isNotEmpty,
                                              child: Container(
                                                constraints: BoxConstraints(
                                                  maxWidth: ScreenService.width*0.7,
                                                ),
                                                padding: EdgeInsets.all(10),
                                                decoration: BoxDecoration(
                                                  color: Color(0xff3F2C9F),
                                                  borderRadius: BorderRadius.circular((10)),
                                                ),
                                                child: Text(
                                                  listchat[index].content,
                                                  strutStyle: StrutStyle(
                                                      forceStrutHeight: true,
                                                      height: 1.2),
                                                  style: TextStyle(
                                                    color: Colors.white,
                                                    fontSize: 13,
                                                  ),
                                                ),
                                              )),
                                          Visibility(
                                              visible: listchat[index]
                                                  .video.isNotEmpty,
                                              child: Container(
                                                padding: EdgeInsets.all(10),
                                                decoration: BoxDecoration(
                                                  color: Color(0xff3F2C9F),
                                                  borderRadius:
                                                  BorderRadius.circular((10))
                                                ),
                                                child: Row(
                                                  mainAxisAlignment: MainAxisAlignment.end,
                                                  children: [
                                                    yiquxiao(index),
                                                    Icon(Icons.videocam,color: Colors.white,)
                                                  ],
                                                ),
                                              )),
                                          // Visibility(
                                          //   visible:
                                          //   listchat[index].img.isNotEmpty,
                                          //   child: GestureDetector(
                                          //     onTap: () {
                                          //       FocusScope.of(context)
                                          //           .requestFocus(FocusNode());
                                          //       Navigator.push(
                                          //           context,
                                          //           new MaterialPageRoute(
                                          //             builder: (context) =>
                                          //             new ChatPhotoShow(
                                          //                 pic:
                                          //                 Platform.isAndroid?listchat[index].img:
                                          //                 '${Util.package}${listchat[index].img}'
                                          //             ),
                                          //           ));
                                          //     },
                                          //     child: Container(
                                          //       decoration: BoxDecoration(
                                          //         color: Color(0xff3F2C9F),
                                          //         borderRadius:
                                          //         BorderRadius.only(
                                          //             topLeft:
                                          //             Radius.circular(
                                          //                 10),
                                          //             bottomLeft:
                                          //             Radius.circular(
                                          //                 10),
                                          //             bottomRight:
                                          //             Radius.circular(
                                          //                 10)),
                                          //       ),
                                          //       constraints: BoxConstraints(
                                          //           maxHeight:
                                          //           MediaQuery.of(context)
                                          //               .size
                                          //               .height *
                                          //               0.16,
                                          //           maxWidth:
                                          //           MediaQuery.of(context)
                                          //               .size
                                          //               .width /
                                          //               3),
                                          //       padding: EdgeInsets.all(2),
                                          //       child: ClipRRect(
                                          //         borderRadius:
                                          //         BorderRadius.only(
                                          //             topLeft:
                                          //             Radius.circular(
                                          //                 10),
                                          //             bottomLeft:
                                          //             Radius.circular(
                                          //                 10),
                                          //             bottomRight:
                                          //             Radius.circular(
                                          //                 10)),
                                          //         child: Image.file(
                                          //           File(
                                          //               Platform.isAndroid?listchat[index].img:
                                          //               '${Util.package}${listchat[index].img}'
                                          //           ),
                                          //           fit: BoxFit.fill,
                                          //           filterQuality:
                                          //           FilterQuality.none,
                                          //         ),
                                          //       ),
                                          //     ),
                                          //   ),
                                          // ),
                                          // Visibility(
                                          //     visible: listchat[index]
                                          //         .yuying.isNotEmpty,
                                          //     child: Container(
                                          //       padding: EdgeInsets.all(10),
                                          //       decoration: BoxDecoration(
                                          //         color: Color(0xff9A44FD),
                                          //         borderRadius:
                                          //         BorderRadius.only(
                                          //             topLeft:
                                          //             Radius.circular(
                                          //                 10),
                                          //             bottomLeft:
                                          //             Radius.circular(
                                          //                 10),
                                          //             bottomRight:
                                          //             Radius.circular(
                                          //                 10)),
                                          //       ),
                                          //       child: Row(
                                          //         mainAxisAlignment: MainAxisAlignment.end,
                                          //         children: [
                                          //           yiquxiao_yuying(index),
                                          //           Icon(Icons.keyboard_voice,color: Colors.white,)
                                          //         ],
                                          //       ),
                                          //     )),
                                          // Visibility(
                                          //     visible: listchat[index]
                                          //         .voice
                                          //         .isNotEmpty,
                                          //     child: GestureDetector(
                                          //       onTap: () {
                                          //         bus.emit('onoff', index);
                                          //       },
                                          //       child: VoiceUIRight(
                                          //           Platform.isAndroid?listchat[index].voice:'${Util.package}${listchat[index].voice}',
                                          //           listchat[index].voicetime == null ? 0 : listchat[index].voicetime,
                                          //           index),
                                          //     )),
                                          // Visibility(
                                          //     visible: listchat[index].gift.isNotEmpty,
                                          //     child: Container(
                                          //         padding: EdgeInsets.all(10),
                                          //         decoration: BoxDecoration(
                                          //           color: Color(0xff9A44FD),
                                          //           borderRadius:
                                          //           BorderRadius.only(
                                          //               topLeft:
                                          //               Radius.circular(
                                          //                   10),
                                          //               bottomLeft:
                                          //               Radius.circular(
                                          //                   10),
                                          //               bottomRight:
                                          //               Radius.circular(
                                          //                   10)),
                                          //         ),
                                          //         child: Row(
                                          //           mainAxisAlignment: MainAxisAlignment.end,
                                          //           children: [
                                          //             CachedNetworkImage(
                                          //               imageUrl: '${listchat[index].gift.split('-X-')[0]}?x-oss-process=image/resize,h_200',
                                          //               height: 50,width: 50,
                                          //               fit: BoxFit.fill,
                                          //               filterQuality: FilterQuality.none,
                                          //             ),
                                          //             Text(
                                          //               '${S.of(context).sent} ${listchat[index].giftname} ${listchat[index].sum}',
                                          //               strutStyle: StrutStyle(
                                          //                   forceStrutHeight: true,
                                          //                   height: 1.2),
                                          //               style: TextStyle(
                                          //                 color: Colors.white,
                                          //                 fontSize: 13,
                                          //               ),
                                          //             ),
                                          //           ],
                                          //         )
                                          //     )
                                          // ),
//                                    Container(
//                                      margin: EdgeInsets.only(top: 5),
//                                      child: Text(
//                                        listchat[index].dateTime,
//                                        style: TextStyle(
//                                          color: Colors.grey,
//                                          fontSize: 10,
//                                        ),
//                                      ),
//                                    ),
                                        ],
                                      ),
                                    )
//                            ClipOval(
//                              child: CachedNetworkImage(
//                                imageUrl: MyInfo.icon,
//                                width: 45,
//                                height: 45,
//                                fit: BoxFit.cover,
//                              ),
//                            ),
                                  ],
                                ),
                              );
                            } else {
                              return Container(
                                width: MediaQuery.of(context).size.width,
                                padding: EdgeInsets.only(
                                    bottom: 15,
                                    right:
                                    MediaQuery.of(context).size.width * 0.2),
                                child: Row(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: <Widget>[
                                    Container(
                                      margin:
                                      EdgeInsets.only(left: 10, top: 10),
                                      child: Column(
                                        crossAxisAlignment:
                                        CrossAxisAlignment.start,
                                        children: <Widget>[
                                          Visibility(
                                            visible: listchat[index]
                                                .content
                                                .isNotEmpty,
                                            child: Container(
                                              constraints: BoxConstraints(
                                                maxWidth: ScreenService.width*0.55,
                                              ),
                                              padding: EdgeInsets.all(10),
                                              decoration: BoxDecoration(
                                                color: Color(0xff2C264C),
                                                borderRadius: BorderRadius.circular((10)),
                                              ),
                                              child: Text(
                                                "${listchat[index].content}",
                                                strutStyle: StrutStyle(
                                                    forceStrutHeight: true,
                                                    height: 1.2),
                                                style: TextStyle(
                                                  color: Colors.white,
                                                  fontSize: 13,
                                                ),
                                              ),
                                            ),
                                          ),
                                          Visibility(
                                              visible: listchat[index]
                                                  .video
                                                  .isNotEmpty,
                                              child: Container(
                                                padding: EdgeInsets.all(10),
                                                decoration: BoxDecoration(
                                                  color: Color(0xff2C264C),
                                                  borderRadius: BorderRadius.circular((10)),
                                                ),
                                                child: Row(
                                                  mainAxisAlignment: MainAxisAlignment.start,
                                                  children: [
                                                    Icon(Icons.videocam,color: Color(0xff9A44FD),),
                                                    yiquxiao2(index),
                                                  ],
                                                ),
                                              )),
                                          // Visibility(
                                          //   visible:
                                          //   listchat[index].img.isNotEmpty,
                                          //   child: GestureDetector(
                                          //     onTap: () {
                                          //       FocusScope.of(context)
                                          //           .requestFocus(FocusNode());
                                          //       Navigator.push(
                                          //           context,
                                          //           new MaterialPageRoute(
                                          //             builder: (context) =>
                                          //             new ChatPhotoShow(
                                          //               pic:
                                          //               listchat[index].img,
                                          //             ),
                                          //           ));
                                          //     },
                                          //     child: Container(
                                          //         decoration: BoxDecoration(
                                          //           color: Color(0xff2C264C),
                                          //           borderRadius:
                                          //           BorderRadius.only(
                                          //               topRight:
                                          //               Radius.circular(
                                          //                   10),
                                          //               bottomLeft:
                                          //               Radius.circular(
                                          //                   10),
                                          //               bottomRight:
                                          //               Radius.circular(
                                          //                   10)),
                                          //         ),
                                          //         constraints: BoxConstraints(
                                          //             maxHeight:
                                          //             MediaQuery.of(context)
                                          //                 .size
                                          //                 .height *
                                          //                 0.16,
                                          //             maxWidth:
                                          //             MediaQuery.of(context)
                                          //                 .size
                                          //                 .width /
                                          //                 3),
                                          //         padding: EdgeInsets.all(2),
                                          //         child: ClipRRect(
                                          //           borderRadius:
                                          //           BorderRadius.circular(
                                          //               (5)),
                                          //           child: Image.file(
                                          //             File(
                                          //                 Platform.isAndroid?listchat[index].img:
                                          //                 '${Util.package}${listchat[index].img}'
                                          //             ),
                                          //             fit: BoxFit.fill,
                                          //           ),
                                          //         )),
                                          //   ),
                                          // ),

                                          // Visibility(
                                          //     visible: listchat[index]
                                          //         .yuying
                                          //         .isNotEmpty,
                                          //     child: Container(
                                          //       padding: EdgeInsets.all(10),
                                          //       decoration: BoxDecoration(
                                          //         color: Colors.white,
                                          //         borderRadius:
                                          //         BorderRadius.only(
                                          //             topRight:
                                          //             Radius.circular(
                                          //                 10),
                                          //             bottomLeft:
                                          //             Radius.circular(
                                          //                 10),
                                          //             bottomRight:
                                          //             Radius.circular(
                                          //                 10)),
                                          //       ),
                                          //       child: Row(
                                          //         mainAxisAlignment: MainAxisAlignment.start,
                                          //         children: [
                                          //           Icon(Icons.keyboard_voice,color: Color(0xff9A44FD),),
                                          //           yiquxiao2_yuying(index),
                                          //         ],
                                          //       ),
                                          //     )),
                                          // Visibility(
                                          //     visible: listchat[index]
                                          //         .voice
                                          //         .isNotEmpty,
                                          //     child: GestureDetector(
                                          //       onTap: () {
                                          //         bus.emit('onoff', index);
                                          //       },
                                          //       child: VoiceUILeft(
                                          //           Platform.isAndroid?listchat[index].voice:
                                          //           '${Util.package}${listchat[index].voice}',
                                          //           listchat[index].voicetime == null ? 0 : listchat[index].voicetime,
                                          //           index),
                                          //     )),
                                          // Visibility(
                                          //     visible: listchat[index].gift.isNotEmpty,
                                          //     child: Container(
                                          //         padding: EdgeInsets.all(10),
                                          //         decoration: BoxDecoration(
                                          //           color: Colors.white,
                                          //           borderRadius:
                                          //           BorderRadius.only(
                                          //               topRight:
                                          //               Radius.circular(
                                          //                   10),
                                          //               bottomLeft:
                                          //               Radius.circular(
                                          //                   10),
                                          //               bottomRight:
                                          //               Radius.circular(
                                          //                   10)),
                                          //         ),
                                          //         child: Row(
                                          //           children: [
                                          //             CachedNetworkImage(
                                          //               imageUrl: '${listchat[index].gift.split('-X-')[0]}?x-oss-process=image/resize,h_200',
                                          //               height: 50,width: 50,
                                          //               fit: BoxFit.fill,
                                          //               filterQuality: FilterQuality.none,
                                          //             ),
                                          //             Text(
                                          //               '${S.of(context).sent} ${listchat[index].giftname} ${listchat[index].sum}',
                                          //               strutStyle: StrutStyle(
                                          //                   forceStrutHeight: true,
                                          //                   height: 1.2),
                                          //               style: TextStyle(
                                          //                 color: MyColor.zhutise,
                                          //                 fontSize: 13,
                                          //               ),
                                          //             ),
                                          //           ],
                                          //         )
                                          //     )
                                          // ),
//                                    Container(
//                                      margin: EdgeInsets.only(top: 5),
//                                      child: Text(
//                                        listchat[index].dateTime,
//                                        style: TextStyle(
//                                          color: Colors.grey,
//                                          fontSize: 10,
//                                        ),
//                                      ),
//                                    ),
                                        ],
                                      ),
                                    )
                                  ],
                                ),
                              );
                            }
                          })
                    ],
                  ),
                )
            ),),
            Container(
              height: 50,
              margin: EdgeInsets.only(bottom: 5),
              decoration: BoxDecoration(
                  color: Color(0xff292637),
                borderRadius: BorderRadius.only(
                    bottomLeft: Radius.circular(10),
                    bottomRight: Radius.circular(10)),
              ),
              child: Center(
                child: TextField(
                  onChanged: (value) {
                    content = value;
                  },
                  textInputAction: TextInputAction.send,
                  onEditingComplete: (){
                    send();
                  },
                  controller: textEditingController,
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: 18
                  ),
                  decoration: InputDecoration(
                    border: InputBorder.none,
                    hintText: 'Send message...',
                    hintStyle: TextStyle(
                        color: Colors.grey, fontSize: 12),
                    contentPadding: EdgeInsets.only(left: 20, right: 20),
                  ),
                ),
              ),
            ),
            // Container(
            //   width: MediaQuery.of(context).size.width,
            //   margin: EdgeInsets.only(
            //       left: 10, right: 10, top: 5, bottom: 20),
            //   child: Row(
            //     mainAxisAlignment: MainAxisAlignment.spaceBetween,
            //     children: <Widget>[
            //       GestureDetector(
            //         behavior: HitTestBehavior.opaque,
            //         onTap: sendImg,
            //         child: Image.asset(
            //           'assets/image/send_pic.png',
            //           width: 30,
            //           height: 30,
            //         ),
            //       ),
            //       GestureDetector(
            //         behavior: HitTestBehavior.opaque,
            //         onTap: sendCam,
            //         child: Image.asset(
            //           'assets/image/send_cam.png',
            //           width: 30,
            //           height: 30,
            //         ),
            //       ),
            //       GestureDetector(
            //         behavior: HitTestBehavior.opaque,
            //         onTap: OpenVoice,
            //         child: Image.asset(
            //           'assets/image/send_voice.png',
            //           width: 30,
            //           height: 30,
            //         ),
            //       ),
            //       GestureDetector(
            //         behavior: HitTestBehavior.opaque,
            //         onTap: sendvideo,
            //         child: Image.asset(
            //           'assets/image/send_mv.png',
            //           width: 30,
            //           height: 30,
            //         ),
            //       ),
            //       GestureDetector(
            //         behavior: HitTestBehavior.opaque,
            //         onTap: () {
            //           Map<String, dynamic> map = {
            //             'yuid': widget.userTable.uid,
            //             'vip' : MyInfo.vip
            //           };
            //           Util.sendAnalyticsActionEvent('CHAT_GIFT',map: map);
            //           if(MyInfo.vip != 0) {
            //             FocusScope.of(context).requestFocus(FocusNode());
            //             showvoice = false;
            //             setState(() {});
            //             gift();
            //           } else {
            //             Navigator.of(context).push(
            //                 MaterialPageRoute(builder: (context)=>BuyVip()));
            //           }
            //         },
            //         child: Image.asset(
            //           'assets/image/send_gift.png',
            //           width: 35,
            //           height: 35,
            //         ),
            //       ),
            //     ],
            //   ),
            // ),
            // Visibility(
            //   visible: showvoice,
            //   child: GestureDetector(
            //     onLongPress: getVoice,
            //     onLongPressUp: sendVoice,
            //     child: Container(
            //       height: MediaQuery.of(context).size.height * 0.3,
            //       margin:
            //           EdgeInsets.only(left: 10, right: 10, bottom: 20),
            //       child: Stack(
            //         children: <Widget>[
            //           Align(
            //             alignment: Alignment.center,
            //             child: Image.asset(
            //               'assets/image/luyin.png',
            //               width: 100,
            //               height: 100,
            //             ),
            //           ),
            //           Visibility(
            //             visible: showvoice2,
            //             child: Align(
            //               alignment: Alignment.center,
            //               child: Image.asset(
            //                 'assets/image/luyin2.png',
            //                 width: 130,
            //                 height: 130,
            //               ),
            //             ),
            //           ),
            //           Align(
            //               alignment: Alignment.topCenter,
            //               child: Container(
            //                 margin: EdgeInsets.only(top: 15),
            //                 child: Text(
            //                   showvoice2 ? '$fens:$miaos' : '${S.of(context).hold}',
            //                   style: TextStyle(
            //                       color: Colors.grey[700], fontSize: 12),
            //                 ),
            //               )),
            //         ],
            //       ),
            //     ),
            //   ),
            // ),
          ],
        ),
      ),
    );
  }

  send(){
    if (myInfo.vip != 0) {
      if (content.isNotEmpty) {
        Msg msg = Msg();
        msg.img = '';
        msg.content = content;
        msg.video ='';
        msg.times =
            DateTime.now().millisecondsSinceEpoch;
        msg.type = 0;
        msg.state = 0;
        msg.uid = widget.userTable.uid;
        db.saveItemMsg(msg);
        util.sendTextMessage(content);
        listchat.add(msg);
        textEditingController.text = '';
        content = '';
        refresh();
        setState(() {});
      }
    } else {
      go_buyvip();
    }
  }

  Widget yiquxiao (int index) {
    if(listchat[index].video.endsWith('0 s')) {
      return Text(
        "  Call cancelled  ",
        strutStyle: StrutStyle(
            forceStrutHeight: true,
            height: 1.2),
        style: TextStyle(
          color: Colors.white,
          fontSize: 13,
        ),
      );
    } else {
      return Text(
        " Chat time ${listchat[index].video}",
        strutStyle: StrutStyle(
            forceStrutHeight: true,
            height: 1.2),
        style: TextStyle(
          color: Colors.white,
          fontSize: 13,
        ),
      );
    }
  }


  Widget yiquxiao2 (int index) {
    if(listchat[index].video.endsWith('0 s')) {
      return Text(
        "  Call cancelled ",
        strutStyle: StrutStyle(
            forceStrutHeight: true,
            height: 1.2),
        style: TextStyle(
          color: Colors.white,
          fontSize: 13,
        ),
      );
    } else {
      return Text(
        " Chat time ${listchat[index].video}",
        strutStyle: StrutStyle(
            forceStrutHeight: true,
            height: 1.2),
        style: TextStyle(
          color: Colors.white,
          fontSize: 13,
        ),
      );
    }
  }


  Widget _icon(int type) {
    if (widget.userTable.icon != '') {
      return CachedNetworkImage(
        placeholder: (context, url) => Container(
          color: Color(0xff272443),
          child: Center(
            child: Image.asset(
              'assets/image/space_bg.png',
              width: type == 1?30:60,
              height: type == 1?30:60,
            ),
          ),
        ),
        imageUrl: widget.userTable.icon,
        fit: BoxFit.cover,
        filterQuality: FilterQuality.none,
      );
    } else {
      return Container(
        color: Color(0xff272443),
        child: Center(
          child: Image.asset(
            'assets/image/space_bg.png',
            width: type == 1?30:60,
            height: type == 1?30:60,
          ),
        ),
      );
    }
  }

  sendvideo() async {
    Map<String, dynamic> map = {
      'yuid': widget.userTable.uid,
      'vip' : myInfo.vip
    };
    if(myInfo.gold < widget.userTable.price) {
      Navigator.of(context)
          .push(MaterialPageRoute(builder: (context) => BuyGold()));
    } else {
      FocusScope.of(context).requestFocus(FocusNode());
      setState(() {});
      util.requestCamera(context).then((value) {
        if (value) {
          util.requestVoice(context).then((value) {
            if (value) {
              Navigator.of(context).push(MaterialPageRoute(builder: (context) {
                return VideoPage(
                  true,
                  price:widget.userTable.price,
                  uid: widget.userTable.uid,
                  role: ClientRole.Broadcaster,
                  channelName: '${myInfo.uid}_${widget.userTable.uid}',
                  nickname: widget.userTable.nickname,
                  icon: widget.userTable.icon,
                  country: widget.userTable.country,
                  countrycode: widget.userTable.code,
                  vip: widget.userTable.vip,
                  birth: widget.userTable.birth,
                );
              }));
            } else {
              util.showToast('Microphone permission required');
            }
          });
        } else {
          util.showToast('Camera permission required');
        }
      });
    }
  }

  refresh() {
    bus.emit('refresh', 1);
  }

  reblack() {
    showModalBottomSheet(
      context: context,
      builder: (BuildContext context) {
        return new Container(
          height: 101.0,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              GestureDetector(
                behavior: HitTestBehavior.opaque,
                onTap: () {
                  Navigator.pop(context);
                  Navigator.push(
                      context,
                      MaterialPageRoute(
                          builder: (context) => Report(widget.userTable.uid)));
                },
                child: Container(
                  height: 50,
                  alignment: Alignment.center,
                  width: MediaQuery.of(context).size.width,
                  child: Text(
                    'Report',
                    style: TextStyle(
                        color: Colors.black,
                        fontSize: 16,
                        fontWeight: FontWeight.bold),
                  ),
                ),
              ),
              Container(
                width: MediaQuery.of(context).size.width,
                height: 1,
                color: Colors.grey[200],
              ),
              GestureDetector(
                behavior: HitTestBehavior.opaque,
                onTap: getBlack,
                child: Container(
                    height: 50,
                    width: MediaQuery.of(context).size.width,
                    child: Center(
                      child: Text(
                        'Block',
                        style: TextStyle(
                            color: Colors.black,
                            fontSize: 16,
                            fontWeight: FontWeight.bold),
                      ),
                    )),
              )
            ],
          ),
        );
      },
    );
  }

  getBlack() async {
    SharedPreferences spfs = await SharedPreferences.getInstance();
    List<String> list = spfs.getStringList('black') ?? [];
    list.add(widget.userTable.uid.toString());
    spfs.setStringList('black', list);
    util.showToast('Successful operation');
    Navigator.pop(context);
  }

  go_buyvip() {
    showModalBottomSheet(
      context: context,
      isScrollControlled: true,
      backgroundColor: Colors.transparent,
      builder: (BuildContext context) {
        return BuyVip(index: 4,);
      },
    );
  }

  @override
  void dispose() {
    upState();
    textEditingController.dispose();
    scrollController.dispose();
    super.dispose();
  }
}
